package com.yylending.las.personal.bandcard;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.Loader;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.yyfax.infrastructure.mvp.Presenter;
import com.yyfax.infrastructure.mvp.PresenterLoader;
import com.yylending.las.Global;
import com.yylending.las.R;
import com.yylending.las.base.BaseFragment;
import com.yylending.las.Constants;
import com.yylending.las.StatIDSet;
import com.yyfax.infrastructure.statistics.StatService;
import com.yylending.las.lending.quota.baseinfo.CustomerInfoPresenter;
import com.yylending.las.lending.quota.entity.CustomerBean;

/**
 * 添加银行卡入口界面.
 * Created by ying_he on 2017/6/14.<br>
 * Copyright  2017 www.yylending.com. All Rights Reserved.<br><br>
 */

public class ManageBankCardFragment extends BaseFragment implements CustomerInfoPresenter.View {

    private CustomerInfoPresenter mPresenter;

    public static BaseFragment newInstance() {
        BaseFragment fragment = new ManageBankCardFragment();
        return fragment;
    }


    @Override
    public void onLoadFinished(Loader<Presenter> loader, Presenter data, int placeholder) {
        mPresenter = (CustomerInfoPresenter) data;
        mPresenter.attach(this);
    }

    @Override
    public Loader<Presenter> onCreateLoader(int id, Bundle args) {
        return new PresenterLoader<>(getActivity(), new CustomerInfoPresenter.Generator());
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_manage_card, null);
        View addBankCardView = view.findViewById(R.id.add_bank_card_btn);
        addBankCardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
				StatService.onEvent(getContext(), StatIDSet.EVENT_ID_ADD_BANK_CARD);
				mPresenter.getCustomerInfo(Constants.CUSTOMER_TYPE_IDENTITY);
            }
        });
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        getActivity().getSupportLoaderManager().initLoader(BankCardActivity.LOADER_ID_GET_REAL_NAME_VERIFY, null, this);
    }

	@Override
	public void customerInfoExist(String type, CustomerBean customerBean) {
		if(Constants.CUSTOMER_TYPE_IDENTITY.equals(type)){
			Global.LOGIN_REAL_NAME = customerBean.identity.iname;
			((BankCardActivity) getActivity()).doForwardOrBack(false);
		}
	}

	@Override
	public void customerInfoNotExist(String type, String msg) {
		if(Constants.CUSTOMER_TYPE_IDENTITY.equals(type)){
			showOneBtnDialog(getString(R.string.should_real_name_before_bind_card), getString(R.string.got_it));
		}
	}

	@Override
	public void getCustomerInfoError(String type, String msg) {
		if(Constants.CUSTOMER_TYPE_IDENTITY.equals(type)) {
            showToast(msg);
		}
	}

	@Override
    public void onDestroyView() {
        super.onDestroyView();
        getActivity().getSupportLoaderManager().destroyLoader(BankCardActivity.LOADER_ID_GET_REAL_NAME_VERIFY);
    }
}
